package cn.tianyigps.tianyigpspublic.rest;

import cn.tianyigps.tianyigpspublic.bean.BaseStation;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;

import java.util.*;

/**
 * Created with IntelliJ IDEA
 * Created By yangxl
 * Date: 2018/6/15
 * Time: 18:11
 * Description:获取基站定位的定位信息和wifi定位的定位信息
 */
@Component
@Slf4j
public class RestLocation {
    @Autowired
    RestTemplate restTemplate;

    // 根据基站数据获取定位信息接口
    public static final String LOCATION_BY_BASESTATION_URL = "http://api.haoservice.com/api/getlbs?mcc=460&mnc=0&cell_id={cell_id}&lac={lac}&type=2&key={key}";
    // 根据wifi数据获取定位信息接口
    public static final String LOCATION_BY_WIFI_URL = "http://apilocate.amap.com/position?key={key}&accesstype=1&macs={macs}";

    /**
     * @Description: 根据查询返回BaseStation对象
     * @params: [lac, cellId]
     * @return: cn.tianyigps.tianyigpsmongopojo.api.BaseStation
     * @date: 2018/6/15 18:14
     */
    public BaseStation getBaseStationByRestApi(String lac, String cellId) {
        Map map = new HashMap();
        map.put("cellId", cellId);
        map.put("lac", lac);
        map.put("key", "128ade83cb924eb5bdd02ba9bc6e151f");
        String longitude = null;
        String latitude = null;
        ObjectMapper mapper = new ObjectMapper();
        try {
            ResponseEntity<String> responseEntity = restTemplate.getForEntity(LOCATION_BY_BASESTATION_URL, String.class, map);
            JsonNode jsonNode = mapper.readTree(responseEntity.getBody());
            JsonNode location =  jsonNode.get("location");
            longitude = String.valueOf(location.get("longitude"));
            latitude = String.valueOf(location.get("latitude"));
        } catch (Exception e) {
            log.error("基站数据转换失败 " + e.getMessage());
            log.error("基站数据获取或转换失败 lac " + lac+" , cellId "+cellId);
            return null;
        }
        if (!StringUtils.isEmpty(longitude) && !StringUtils.isEmpty(latitude)) {
            BaseStation baseStation = new BaseStation();
            baseStation.setId(lac + cellId);
            baseStation.setLac(lac);
            baseStation.setCellId(cellId);
            baseStation.setPoints(new GeoJsonPoint(Double.parseDouble(longitude), Double.parseDouble(latitude)));
            baseStation.setTime(new Date());
            return baseStation;
        }
        log.error("经纬度未解析成功 ：lng"+longitude+"  lat" + latitude);
        return null;
    }

    /**
     * @Description: 根据wifi热点数据数据获取经纬度
     * @params: [macs]
     * @return: java.util.List
     * @date: 2018/6/19 13:22
     */
    public List<String> getWifiMessageByRestApi(String macs) {
        Map map = new HashMap();
        map.put("macs", macs);
        //高德地图的api的key
        map.put("key", "be62023ca3fab1d16c0dd33bf697b1c7");
        ObjectMapper mapper = new ObjectMapper();
        String[] loc = null;
        try {
            ResponseEntity<String> responseEntity = restTemplate.getForEntity(LOCATION_BY_WIFI_URL, String.class, map);
            JsonNode jsonNode = mapper.readTree(responseEntity.getBody());
            JsonNode result = jsonNode.get("result");
            String location = String.valueOf(result.get("location")).replaceAll("\"", "");
            loc = location.split(",");
        } catch (Exception e) {
            log.error("wifi数据转换失败 "+ e.getMessage());
            log.error("wifi数据获取或转换失败 macs"+ macs);
        }
        return (Optional.ofNullable(loc).isPresent() && loc.length > 0 )? Arrays.asList(loc) : null;
    }
}
